package com.example.androidproject.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener2
import android.hardware.SensorManager
import android.net.ConnectivityManager
import android.os.Looper
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.android.baselibrary.base.BaseActivity
import com.android.baselibrary.utils.GridSpaceItemDecoration
import com.cms.breakpad.BreakPadMainActivity
import com.example.androidproject.BarUtils
import com.example.androidproject.LogerPrinter
import com.example.androidproject.R
import com.example.androidproject.activity.utils.showDarkStatusBar
import com.example.androidproject.activity.utils.showFitsSystemWindow
import com.example.androidproject.adapter.GridAdapter
import com.example.androidproject.bean.DataBean
import com.example.androidproject.bean.DataBeanUtils
import com.example.androidproject.databinding.ActivityMainBinding
import com.example.androidproject.fps.FpsMonitor
import com.example.androidproject.receiver.NetWorkReceiver
import java.util.*


class MainActivity : BaseActivity<ActivityMainBinding>(){
    private var mGridAdapter: GridAdapter?=null
    val TAG = "MainActivity_A"

    private var fpsMonitor = FpsMonitor()

    override fun getLayoutId(): Int  = R.layout.activity_main
    private var dataList = DataBeanUtils.dataList
    private var mDataList :MutableList<DataBean> = mutableListOf<DataBean>()
    companion object{
        @SuppressLint("StaticFieldLeak")
        var instance:Context?=null
    }
    override fun initView() {
        showFitsSystemWindow(true)
        showDarkStatusBar()

        Log.i("TAG1111", "initView: ${dataList.size}")
        dataList.forEach {
            mDataList.add(DataBean(it.name!!,it.path!!,false))
        }


        mGridAdapter = GridAdapter()
        mBinding.recycleView.addItemDecoration(GridSpaceItemDecoration(3,15))
        mBinding.recycleView.adapter = mGridAdapter

        mGridAdapter?.hasStableIds()
        mGridAdapter?.setData(mDataList)
        instance =this


        val list = mutableListOf<String>()
        list.add("1")
        list.add("2")
        list.add("3")
        list.add("4")
       /* testList.forEach {
            if (it == "3"){
                return
            }
            Log.i("TAG1111", "initView: it:$it")
        }*/
        list.run loop@{
           forEach {
               if (it == "3"){
                   return@loop
               }
           }
        }

        Log.i("TAG1111", "initView: 执行结束")


        val navBarHeight = BarUtils.getNavBarHeight()

        Log.i("TAG11112", "initView: 当前手机底部状态栏的高度是:$navBarHeight")

       fpsMonitor.initFpsMonitor()
       /* Timer().schedule(object :TimerTask(){
            override fun run() {
                fpsMonitor.start()
            }
            },1000L,1000L)*/
    }





    override fun initListener() {
        mGridAdapter?.setOnClickListener { data, _ ->
            Log.i("TAGss", "d点击事件 ")
            //登录拦截

           if (data.path!!.isBlank()) return@setOnClickListener
            //正常跳转
            if (data.name.equals("BreakPad")){
              startActivity(Intent(this,BreakPadMainActivity::class.java))
            }else{
                ARouter.getInstance().build(data.path).navigation()
            }


          //  BottomDialog().show(supportFragmentManager,"fff")

        //登录拦截

          //  ARouter.getInstance().build(data.path).navigation(this, LoginNavigationCallbackImpl())
            //获取服务
//            val designModelProvider =
//                ARouter.getInstance().build(ServicePath.DESIGNMODELPROVIDER).navigation() as DesignModelProvider

           // Log.i("TAGss", "获取数据${designModelProvider.getDesignModelName()} ")
        }

    /*  val sender = object :OrientationEventListener(this){
          override fun onOrientationChanged(orientation: Int) {
              Log.i("TAG121", "onOrientationChanged: orientation:$orientation")
          }
      }
       if (sender.canDetectOrientation()){
           sender.enable()
       } else{
           ToastUtils.show(this,"不支持传感器")
       }*/
        /**
         * 传感器获取
         */
       val manager =  getSystemService(Context.SENSOR_SERVICE) as SensorManager
        Log.i("TAG123", "onSensorChanged1: "+manager.getSensorList(Sensor.TYPE_ALL))
        manager.registerListener(object :SensorEventListener2{
            override fun onSensorChanged(event: SensorEvent?) {
                Log.i("TAG123", "onSensorChanged: ")
            }

            override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
                Log.i("TAG123", "onAccuracyChanged: ")
            }

            override fun onFlushCompleted(sensor: Sensor?) {
                Log.i("TAG123", "onFlushCompleted: ")
            }
        },manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL)


        Looper.getMainLooper().setMessageLogging(LogerPrinter())


    }
    var network:NetWorkReceiver?=null
    override fun onResume() {
        network = NetWorkReceiver()
        val filter = IntentFilter()
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
        registerReceiver(network,filter)
        super.onResume()
        Log.i(TAG, "onResume: ")
    }

    override fun onPause() {
        unregisterReceiver(network)
        super.onPause()
        Log.i(TAG, "onPause: ")
    }

    override fun onStart() {
        super.onStart()
        Log.i(TAG, "onStart: ")
    }

    override fun onStop() {
        super.onStop()
        Log.i(TAG, "onStop: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i(TAG, "onDestroy: ")
    }


}